/*
 * 2014-11-16 <br/>
 *
 * Copyright (c) 1994-1999 AnHui LonSun, Inc. <br/>
 * All rights reserved.	<br/>
 *
 * This software is the confidential and proprietary information of AnHui	<br/>
 * LonSun, Inc. ("Confidential Information").  You shall not	<br/>
 * disclose such Confidential Information and shall use it only in	<br/>
 * accordance with the terms of the license agreement you entered into	<br/>
 * with Sun. <br/>
 */
package demo;

import demo.util.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResServlet extends HttpServlet {
    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html;charset=UTF-8");
        res.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");
        String action = req.getParameter("action") == null?"": req.getParameter("action");
        String aa = req.getParameter("aa") == null ? "" : req.getParameter("aa");
        if("autocomplete".equals(action)){
            autocomplete(req,res);
        }
        //super.service(req, res);
    }

    /***
     * @param req
     * @param res
     */
    private void autocomplete(ServletRequest req,ServletResponse res) throws IOException {
        String q = req.getParameter("q");
        q = new String(q.getBytes("ISO8859_1"), "UTF-8");
        System.out.println("q===============".concat(q));

        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(0);
        Map<String,Object> data= new HashMap<String, Object>(3);
        data.put("id",100);
        data.put("name","张三");
        data.put("age",22);
        result.add(data);

        data = new HashMap<String, Object>(3);
        data.put("id", 200);
        data.put("name", "李三");
        data.put("age", 33);
        result.add(data);

        data = new HashMap<String, Object>(3);
        data.put("id", 300);
        data.put("name", "王五");
        data.put("age", 13);
        result.add(data);
        JsonUtil jsonUtil = new JsonUtil();
        //res.getWriter().print(jsonUtil.fromObjectToJson(result));
    }
}
